<?php
// +----------------------------------------------------------------------
// | WoShiMaiJia Projcet
// +----------------------------------------------------------------------
// | Copyright (c) 2010 http://woshimaijia.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: xinqiyang <517577550@qq.com>
// +----------------------------------------------------------------------

/**
 * YxqCategory 
 * set the tablename is Category
 * @author xinqiyang
 *
 */
class YxqCategory {
	var $tree=array();
	public function __construct($model='category'){
		$Dao = D($model);
		$vo  = $Dao->order("parentid asc,displayorder desc")->findall();
		//TODO:this is could save the cache for the tree
		$list = list_to_tree($vo,'id','parentid','_child');
		$this->treetoary($list);
		unset($list,$vo,$Dao);
		//parent::_initialize();
	}
    
	/*
	public function index()
	{
		$this->assign('list',$this->tree);
		$this->display();
	}

	public function add() {
	 $sel	=	isset($_GET['parentid'])?$_GET['parentid']:0;
	 $this->assign("cu_parent",$sel);
	 $this->assign("tree",$this->tree);
	 $this->display();
	}

	public function insert()
	{
		// 保存添加的数据
		$Dao = D("Tree");
		$vo = $Dao->create();
		if(false === $vo) {
			$this->error($Dao->getError());
		}
		$rs = $Dao->add();
		if($rs){
			$this->assign("jumpUrl",__URL__);
			$this->success("添加数据成功!");
		}else{
			$this->error("添加数据失败");
		}
	}

	public function edit() {
		$Dao = D("category");
		$vo = $Dao->find("id=".$_GET["id"]);
		if($vo === false)
		{
			$this->assign("jumpUrl",__URL__);
			$this->error("不存在此记录");
		}
		$this->assign("cu_parent",$vo['parentid']);
		$this->assign("data",$vo);
		$this->assign("category",$this->tree);
		$this->display();
	}

	function update()
	{
		// 保存编辑过的数据
		$Dao = D("category");
		$id = $_POST['id'];
		$parent = $_POST['parentid'];
		$or_parent = $_POST['or_parent'];
		if($id==$parent) {
			$_POST['parentid']=$_POST['or_parent'];
		}
		if($parent=='') {
			$_POST['parentid']=$or_parent;
			$parent = $or_parent;
		}
		if($parent!=$or_parent) {
			if($this->ismychild($id,$parent)) {
				$this->error("不能移动到自身的子树下");
			}
		}
		$vo = $Dao->create();
		if(false === $vo) {
			$this->error($Dao->getError());
		}
		$rs = $Dao->save();
		if($rs){
			$this->assign("jumpUrl",__URL__);
			$this->success("保存修改成功!");
		}else{
			$this->error("保存修改失败");
		}
	}

	public function delete() {
	 $err = false;
	 $id_ary=isset($_POST["id"])?$_POST['id']:$_GET['id'];
	 $Dao	=	D("category");
	 if(is_array($id_ary)) {
	 	foreach($id_ary as $val)
	 	{
	 		if(count($this->get_mychild($val))>1) {
	 			$err = true;
	 			break;
	 		}
	 	}
	 }else{
			if(count($this->get_mychild($id_ary))>1) {
				$err = true;
			}
	 }
	 if($err) {$this->error('该栏目下还存在子栏目,不能删除');}
	 if(is_array($id_ary)) {
	 	$str=implode(",",$id_ary);
	 	$Dao->deleteByIds($str);
	 }
	 else
		$Dao->deleteById($id_ary);
		$this->assign('jumpUrl',__URL__."/index");
		$this->success('操作已成功！');
	}
    */
	/**
	 * is my child 
	 * @param $id  father
	 * @param $uid child
	 */
	function is_mychild($id,$uid) {
		if($id==$uid) return false;
		if($uid==0 and $id!=0) return  false;
		$find=false;
		$child=$this->get_mychild($id);
		if(in_array($uid,$child))
		return true;
		else
		return false;
	}

	/**
	 * get my child
	 * @param $id
	 */
	function get_mychild($id) {
		$child = array($id);
		foreach($this->tree as $key =>$val)
		{
			if($id==$val['id']) {
				$find = true;
				break;
			}
		}
		$j=count($this->tree);
		while($key<=$j)
		{
			++$key;
			$t_id = $this->tree[$key]['parentid'];
			if(in_array($t_id,$child))
				array_push($child,$this->tree[$key]['id']);
			else
			break;
		}
		return $child;
	}
	/**
	 * return id's level 1 child nodes
	 * @param $id parentid
	 */
	function get_child($id)
	{
		$child = list_search($this->tree,'parentid='.$id);
		return $child;
	}
	/**
	 * tree to array 
	 * @param array $list arraylist
	 * @param int $level tree level
	 */
	function treetoary($list,$level=0) {
		foreach($list as $key=>$val)
		{
			$tmp_str=str_repeat("&nbsp;",$level*2);
			if($level>0) {
				$tmp_str.="├";
			}
			$val['title']=$tmp_str."&nbsp;".$val['title'];
			if(!array_key_exists('_child',$val)) {
				array_push($this->tree,$val);
			}else{
				$tmp_ary = $val['_child'];
				unset($val['_child']);
				array_push($this->tree,$val);
				$this->treetoary($tmp_ary,$level+1);
			}
		}
		return;
	}

}
?>